package com.biomatch.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.biomatch.domain.ContainerLock;
import com.biomatch.domain.Group;
import com.biomatch.domain.Position;
import com.biomatch.domain.UserInfo;
import com.biomatch.dto.Pagination;
import com.biomatch.mapper.ContainerLockMapper;
import com.biomatch.mapper.GroupMapper;
import com.biomatch.mapper.UserMapper;
import com.biomatch.service.GroupService;
import com.biomatch.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class GroupServiceImpl implements GroupService {

//    @Autowired
//    private ContainerLockMapper containerLockMapper;

    @Autowired
    private GroupMapper groupMapper;

    @Autowired
    private UserMapper userMapper;

    @Override
    public IPage<Group> get4Page(Pagination pagination) {

        Map<String,Object> params = new HashMap<String,Object>();
        params.put("start",(pagination.getCurrent()-1) * pagination.getPageSize());
        params.put("pageSize",pagination.getPageSize());
        //获取总记录数据
        pagination.setTotal(groupMapper.findCountByMap(params));

        //Page<Group> page = new Page<>(pagination.getCurrent(), pagination.getPageSize(),pagination.getTotal());

        //获取查询的数据
       // result.setData(userMapper.findListByMap(params));
        //pagination.setData(groupMapper.findListByMap(params));

        IPage<Group> iPage = new Page<>(pagination.getCurrent(), pagination.getPageSize(),pagination.getTotal());
        //iPage.setTotal(groupMapper.findCountByMap(params));
        iPage.setRecords(groupMapper.selectGroupList(params));

        return iPage;
    }

    @Override
    public List<Group> getAll() {
        return groupMapper.selectList(null);
    }

    @Override
    public Group deleteGroupById(Group group) {
        if(groupMapper.selectById(group.getId())!=null)
        {
            groupMapper.deleteGroupById(group);
            groupMapper.deleteGroupAndUser(group.getGroupId());
            return group;
        }
        else {
            return  null;
        }
    }

    @Override
    public Group saveOrUpdateGroup(Group group) {
            if (group.getId() <= 0 ) {
            QueryWrapper<Group> groupQueryWrapper = new QueryWrapper<>();
            groupQueryWrapper.eq("name", group.getName());
            if (groupMapper.selectCount(groupQueryWrapper) > 0) {
                return null;
            }
            else {
                String code = String.valueOf(new Date().getTime());
                group.setGroupId("G" + code.substring(5, code.length()));
                group.setCreateTime(new Date());
                groupMapper.insert(group);
                List<UserInfo> users = group.getUsers();
                //1.查出所有userinfo
                //2.双重for循环，先users，再从上面的List里找到usercode组合插入
                for (UserInfo user : users) {
                    groupMapper.insertGroupAndUser(group.getGroupId(), user.getUsrcod());
                }
                return group;
            }
        }
        else {
            QueryWrapper<Group> groupQueryWrapper = new QueryWrapper<>();
            groupQueryWrapper.eq("name", group.getName()).ne("GroupId", group.getGroupId());
            if (groupMapper.selectCount(groupQueryWrapper) > 0) {
                return null;
            }
            else {
                group.setUpdateTime(new Date());
                groupMapper.deleteGroupAndUser(group.getGroupId());
                List<UserInfo> users = group.getUsers();
                for (UserInfo user : users) {
                    groupMapper.insertGroupAndUser(group.getGroupId(),user.getUsrcod());
                }
                groupMapper.updateById(group);
                return group;
            }
        }

    }
}
